<?php

namespace app\commission;

use app\models\distribution\DistributionBasic;
use app\models\merchant\MerchantOrder as Model;
use app\models\merchant\MerchantPayFor;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use app\util\Debug;

class Merchant
{
    public function commission(Model $order)
    {
        $transaction = \Yii::$app->db->beginTransaction();

        try {

            $payFor = MerchantPayFor::findOne(['pay_sn' => $order->sn]);

            // pay total - for amount
            $commission = $order->total - $payFor->amount;
            $commission *= 0.5;

            if ($commission > 0) {
                /**
                 * 从"服务中心"开始返,
                 * 极差
                 *
                 * 70% | 40%
                 */
                // $level = [302, 303, 401, 402, 403, 404, 405];
                $level = [
                    UserLevel::$keys['svip2'],
                    UserLevel::$keys['svip3'],
                    UserLevel::$keys['star1'],
                    UserLevel::$keys['star2'],
                    UserLevel::$keys['star3'],
                    UserLevel::$keys['star4']
                ];
                $referrer_list = Chain::getReferrerChain($order->user_id, $level);

                $set = DistributionBasic::getLastOne();

                $childNode = false;
                foreach ($referrer_list as $referrer) {

                    // high level (运营中心+)
                    if ($referrer['level'] > UserLevel::$keys['svip2']) {
                        if (!$childNode) {
                            $balance = $commission * $set->svip3_;
                        } else {
                            $balance = $commission * ($set->svip3_ - $set->svip2);
                        }

                        $this->save($referrer['id'], $balance, $order);
                        break;
                    }

                    // low level (服务中心)
                    if (!$childNode && $referrer['level'] == UserLevel::$keys['svip2']) {
                        $balance = $commission * $set->svip2;
                        $childNode = true;

                        $this->save($referrer['id'], $balance, $order);
                    }
                }
            }

            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            Debug::commission('merchant-fail', $e->getMessage());
        }
    }

    private function save($user_id, $balance, Model $order)
    {
        if ($balance > 0) {
            // save commission
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $order->user_id;
            $model->balance = $balance;
            $model->merchant_order_id = $order->id;
            $model->status = 1;
            $model->award_type = UserCommission::$AWARD_Type['MERCHANT_PAY'];
            $model->award_name = UserCommission::$AWARD_NAME['MERCHANT_PAY'];
            $model->save();

            // save to account
            Commission::getInstance()->commissionSaveAccount($model);
        }
    }
}